package com.diandian.backend.order.service;

import com.diandian.backend.order.domain.OrderOnlineJingdongHomeSource;
import com.diandian.backend.order.dto.*;
import com.diandian.backend.order.mapper.OrderOnlineJingdongHomeSourceMapper;
import com.diandian.backend.order.resp.OrderOnlineJingdongHomeSourceDetailResp;
import com.diandian.base.boot.starter.exception.DefaultException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
public class OrderOnlineJingdongHomeSourceService {

    @Autowired
    private OrderOnlineJingdongHomeSourceMapper orderOnlineJingdongHomeSourceMapper;

    public OrderOnlineJingdongHomeSourceDetailResp getDetail(OrderOnlineJingdongHomeSourceDetailParamDTO param) {

        OrderOnlineJingdongHomeSource dbData = orderOnlineJingdongHomeSourceMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        OrderOnlineJingdongHomeSourceDetailResp result = new OrderOnlineJingdongHomeSourceDetailResp();
        result.setId(dbData.getId());
        result.setBelong(dbData.getBelong());
        result.setOrderNum(dbData.getOrderNum());
        result.setContent(dbData.getContent());
        result.setFinance(dbData.getFinance());
        result.setExtra(dbData.getExtra());
        result.setCreateTime(dbData.getCreateTime());
        result.setDel(dbData.getDel());
        result.setOrgId(dbData.getOrgId());
        result.setOrgCode(dbData.getOrgCode());
        return result;
    }

    public void add(OrderOnlineJingdongHomeSourceAddParamDTO param) {

        // 数据检查
        param.check();

        String orderNum = param.getOrderNum();

        // 查询是否已存在
        OrderOnlineJingdongHomeSourceDetailParamDTO paramDTO = new OrderOnlineJingdongHomeSourceDetailParamDTO();
        paramDTO.setOrderNum(orderNum);
        OrderOnlineJingdongHomeSourceDetailResp dbOrder = getDetail(paramDTO);
        if(dbOrder != null){
            throw new DefaultException("已存在此订单");
        }

        // 不存在时，保存
        param.setId(UUID.randomUUID().toString().replace("-", ""));
        param.setCreateTime(new Date());
        param.setDel(0);
        orderOnlineJingdongHomeSourceMapper.add(param);
    }
}
